package cn.stylefeng.guns.modular.hx.order.utils;

import java.sql.*;

public class MssqlUtils {

    private static final String DBDRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";

    /**
     * 链接SQL server数据库
     *
     * @param ip
     * @param port
     * @param dbName
     * @param user
     * @param pwd
     * @return
     */
    public static Connection getConnection(String ip, Integer port, String dbName, String user, String pwd) throws SQLException, ClassNotFoundException {
        Connection conn = null;
        String DBURL = "jdbc:sqlserver://" + ip + ":" + port + ";databaseName=" + dbName;
        //第一步：加载数据库驱动程序，此时不需要实例化，因为会由容器自己负责
        Class.forName(DBDRIVER);
        //第二步：根据连接协议、用户名、密码连接数据库
        conn = DriverManager.getConnection(DBURL, user, pwd);
        return conn;
    }
}
